package com.pug.zixun.config.interceptor;

import com.pug.zixun.commons.anno.IgnoreToken;
import com.pug.zixun.commons.enums.AdminUserResultEnum;
import com.pug.zixun.commons.ex.PugValidatorException;
import com.pug.zixun.commons.utils.fn.asserts.Vsserts;
import com.pug.zixun.config.jwt.JwtService;
import com.pug.zixun.domain.User;
import com.pug.zixun.local.UserThreadLocal;
import com.pug.zixun.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @Author: WengYu
 * @CreateTime: 2022/05/19 10:56
 */
@Component
public class PassportLoginInterceptor implements HandlerInterceptor {

    @Autowired
    private JwtService jwtService;
    @Autowired
    private IUserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // handler从object对象转换成具体的目标对象HandlerMethod
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        // 判断方法和类上是否有 IgnoreToken 注解 有就直接放行
        if (handlerMethod.getMethod().getAnnotation(IgnoreToken.class) != null ||
                handlerMethod.getBeanType().getAnnotation(IgnoreToken.class) != null) {
            return true;
        }
        // 获取用户请求的token
        String token = jwtService.getToken(request);
        // 如果token没有传递，直接走统一异常处理 + 统一返回(内部自动帮你把数据通过jackson转换成json返回)
        Vsserts.isEmptyEx(token, AdminUserResultEnum.TOKEN_NOT_FOUND);
        // 获取请求头的用户id
        String tokenUserId = jwtService.getTokenUserId(request);
        // 校验token是否合法
        boolean refreshTokenRedis = jwtService.refreshTokenRedis(token,tokenUserId,response);
        Vsserts.isFalseEx(refreshTokenRedis, AdminUserResultEnum.TOKEN_ERROR);
        // 解析token,获取用户id
        Vsserts.isNullEx(tokenUserId, AdminUserResultEnum.USER_NULL_ERROR);
        // 根据用户查询用户信息，
        User user = userService.getById(Long.parseLong(tokenUserId));
        Vsserts.isNullEx(user, AdminUserResultEnum.USER_NULL_ERROR);
        // 拉黑处理
        if (user.getForbbiden() != null && user.getForbbiden().equals(0)) {
            throw new PugValidatorException(AdminUserResultEnum.USER_FORBIDDEN_ERROR);
        }
        // 删除处理
        if (user.getIs_delete() != null && user.getIs_delete().equals(1)) {
            throw new PugValidatorException(AdminUserResultEnum.USER_FORBIDDEN_ERROR);
        }
        // 把用户信息放入到UserThreadLocal
        UserThreadLocal.put(user);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        UserThreadLocal.remove();
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserThreadLocal.remove();
    }
}
